package edu.sctu.yueding.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import edu.sctu.yueding.R;
import edu.sctu.yueding.bean.Comments;
import edu.sctu.yueding.view.CircleImageView;

/**
 * Created by 鑫爷您慢点 on 2016/8/19.
 */
public class CommunityContentListViewAdapter extends BaseAdapter {
    private Activity activity;
    private List<Comments> data = new ArrayList<>();
    private View view;
    private ViewHolder viewHolder;
    private LayoutInflater layoutInflater;

    public CommunityContentListViewAdapter(List<Comments> data, Activity activity) {
        this.data = data;
        this.activity = activity;
        layoutInflater = LayoutInflater.from(activity);
    }

    public void onDataChange(List<Comments> data) {
        this.data = data;
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        if (data != null && data.size() > 0) {
            return data.size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            view = layoutInflater.inflate(R.layout.item_community_content_list_view, null);

            viewHolder = new ViewHolder();

            viewHolder.nameTxt = (TextView) view.findViewById(R.id.id_item_community_content_people_name);
            viewHolder.contentTxt = (TextView) view.findViewById(R.id.id_item_community_content);

            view.setTag(viewHolder);//将viewHolder储存在view中
        } else {
            viewHolder = (ViewHolder) view.getTag();
            view = convertView;
        }

        viewHolder.nameTxt.setText(data.get(position).getAuthorName() + " ：");
        viewHolder.contentTxt.setText(data.get(position).getContent());
        return view;
    }

    private class ViewHolder {
        TextView nameTxt, contentTxt;
    }
}
